/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */package org.merak.core.text.json;

public final class ObjectJson {

	//~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	private static final ThreadLocal<JsonContext> ctx = new ThreadLocal<JsonContext>();

	//~ Constructors /////////////////////////////////////////////////////////////
	//****************************************************************************
	private ObjectJson() {
		// This class must not be instanced or sub-classed
	}

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	public static String toJson(Object o) {

		// Get context attached to current running thread
		JsonContext context = ctx.get();
		if (context == null) ctx.set(
			context = new JsonContext()
		);

		// Render the object
		try {
			context.render(o);
			return context.getBuffer().toString();
		}
		catch (Exception ex) {
			throw new RuntimeException(ex);
		}
		finally {
			context.clear();
		}

	}
	//****************************************************************************
	public static String toJson(Object o,JsonfierSet set) {

		// Get context attached to current running thread
		JsonContext context = ctx.get();
		if (context == null) ctx.set(
			context = new JsonContext()
		);

		// Render the object
		try {
			context.render(o,set);
			return context.getBuffer().toString();
		}
		catch (Exception ex) {
			throw new RuntimeException(ex);
		}
		finally {
			context.clear();
		}

	}
	//****************************************************************************

}
